require(['./config'],() => {
    require(['request','template','header','footer'],(req,template) => {
        class Detail{
            constructor(){
                this.getProdId()
            }
            //获取地址栏id
            getProdId(){
                const str = window.location.search.split('=')[1]
                // console.log(str)
                this.getData(str)
            }
            //发送请求
            getData(prodId){
                req.getDetailsData(prodId)
                    .then(res => {
                        if(res.code === 200){
                            console.log(res)
                            const {detail} = res.data 
                            this.renderHtml(detail)
                        }
                    })
            }
            //渲染页面
            renderHtml(res){
                // console.log(res)
                //descContentList商品详情
                //id 商品id
                //photo 放大镜图片
                // title标题
                // price价格
                // saleNum销量
                const {descContentList,id,title,photo,price,saleNum} = res
                $('main').html(template('proodDetailTemplate',{descContentList,id,title,photo,price,saleNum}))
                this.addToCart({id,photo,title,price,saleNum})
            }

            //加入购物车
            addToCart(prodInfo){
                const {id,photo,title,price,saleNum} = prodInfo
                //给购物车添加点击事件
                $('.c-t-b').click(() =>{
                    let prodList = localStorage.getItem('prodList')
                    // console.log(prodList)
                    if(prodList){
                        //有这个数据(prodList)
                        //没有这条数据加一条这数据
                        console.log(id)
                        prodList = JSON.parse(prodList)
                        const flag = prodList.some(item => {
                            return item.id === id
                        })
                        if(flag){
                            prodList.forEach(item => {
                                item.id === id ? item.count++ : ''
                            })
                        }else{
                                prodList.push({
                                    id,
                                    imgSrc:photo[0].url,
                                    title,
                                    price,
                                    saleNum,
                                    count:$('.pass').val() - 0
                                })
                        }
                        //重新保存数据
                        localStorage.setItem('prodList',JSON.stringify(prodList))
                    }else{
                        //没有这个数据
                        const arr = [{id,imgSrc:photo[0].url,title,price,saleNum,count:$('.pass').val()-0}]
                        // console.log(arr)

                        //放到localStorage
                        localStorage.setItem('prodList',JSON.stringify(arr))
                    }
                })
            }
        }
        return new Detail()
    })

})